<?php
include 'connectdb.php';
if (isset($_GET['logout'])){
    session_destroy();
    echo '<script langquage="javascript">window.location="index.php";</script>';
}
else if( isset($_POST['email'] ) && isset ($_POST['pass']) ){
    $q = "select * from customer where email ='".$_POST['email']."' and password ='".$_POST['pass']."'";
    $result = mysqli_query($link, $q);
    $row = mysqli_fetch_array($result);
    if (mysqli_num_rows($result)==0){
        echo '<script langquage="javascript">alert("email หรือ password ไม่ถูกต้อง");</script>';
        echo '<script langquage="javascript">window.location="index.php";</script>';
        exit;
    }
    else{
        $_SESSION["id"] = $row['customer_id'];
        $_SESSION["user"] = $row['customer_fname'];
        $_SESSION["lname"] = $row['customer_lname'];
        echo 'ยินดีต้อนรับ&nbsp;', $_SESSION["user"],'&nbsp;&nbsp;',$_SESSION["lname"];
        echo '<ul><li><a href="index.php?booking_history=true">ประวัติการจอง</li><li><a href="index.php?logout=true">Logout</a></li></ul> ';
        echo '<script langquage="javascript">window.location="index.php";</script>';
    }
}    

if( !empty($_SESSION['user'] )){
    echo 'ยินดีต้อนรับ&nbsp;', $_SESSION["user"],'&nbsp;&nbsp;',$_SESSION["lname"];
    echo '<ul><li><a href="index.php?booking_history=true">ประวัติการจอง</li><li><a href="index.php?logout=true">Logout</a></li></ul> ';
}
else{
?>
    <form name="frmLogin" method="post" action="index.php">
    <table border="1" width="260" height="180">
        <tr>
            <td colspan="2">[เข้าสู่ระบบ]</td>
        </tr>
        <tr>
            <td>อีเมล์ :</td>
            <td><input type="text" name="email"/></td>
        </tr>
        <tr>
            <td>รหัส :</td>
            <td><input type="password" name="pass"/></td>
        </tr>
        <tr>
            <td colspan="2"><input type="submit" name="btnLogin" value="เข้าสู่ระบบ" style='width :250px' class="btn btn-primary"/></td>
        </tr>
        <tr>
            <td colspan="2" align="center"><a href="register.php">ลงทะเบียน</a>&nbsp;|&nbsp;<a href="forgotpassword.php">ลืมรหัสผ่าน?</a></td>
        </tr>
    </table>
    </form>
<?php
}
mysqli_close($link);
?>